<?php

namespace App\Controllers;

use common\enum\CommonEnum;
use Illuminate\Http\Request;
use common\utils\SysCode;
use App\Services\PayChannelService;

/**
 * 支付渠道Controller
 * Class PayController
 * @package App\Controllers
 */
class PayController extends BaseController
{
    // 支付渠道列表
    public function getPayChannelList(Request $request)
    {
        $params = $request->input();
        $params['page'] = isset($params['page']) ? $params['page'] : CommonEnum::PAGE_DEFAULT;
        $params['page_size'] = isset($params['page_size']) ? $params['page_size'] : CommonEnum::PAGE_SIZE_TWENTY;
        if(isset($params['status']) && !in_array($params['status'], [CommonEnum::ENABLED, CommonEnum::DISABLE])){
            error_exit(SysCode::INVALID_ARGUMENT);
        }
        return PayChannelService::getPayChannelList($params);
    }

    // 支付渠道详细
    public function getPayChannelInfo(Request $request)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $id = $request->input('id');
        if(empty($id)){
            error_exit(SysCode::FAILED);
        }
        return PayChannelService::getPayChannelInfo($id);
    }

    // 可选支付通道
    public function getPayChannel()
    {
        return PayChannelService::getPayChannel();
    }

    // 新增支付渠道
    public function addPayChannelInfo(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'code' => 'required',
            'pay_scene_data' => 'required',
            'api_params' => 'required',
            'status' => 'required',
//            'description' => 'required',  // 2008-11-30 产品要求可以为空
        ]);
        if ($request->has('id')) {
            error_exit(SysCode::INVALID_ARGUMENT);
        }
        $params = $request->input();
        if (!isset($params['name']) || empty($params['name'])
            || !isset($params['code']) || empty($params['code'])
            || !isset($params['status']) || !in_array($params['status'], [CommonEnum::ENABLED, CommonEnum::DISABLE])
            || !isset($params['pay_scene_data']) || empty($params['pay_scene_data'])
            || !isset($params['api_params']) || empty($params['api_params'])
        ) {
            error_exit(SysCode::MISS_REQUIRED_PARAMETER);
        }
        if(PayChannelService::savePayChannelInfo($params)){
            return 'true';
        }else{
            return 'false';
        }
    }

    // 修改支付渠道
    public function modifyPayChannelInfo(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'name' => 'required',
            'code' => 'required',
            'pay_scene_data' => 'required',
            'api_params' => 'required',
            'status' => 'required',
//            'description' => 'required',// 2008-11-30 产品要求可以为空
        ]);
        $params = $request->input();
        if (!isset($params['id']) || empty($params['id']) || !isset($params['name']) || empty($params['name'])
            || !isset($params['code']) || empty($params['code'])
            || !isset($params['status']) || !in_array($params['status'], [CommonEnum::ENABLED, CommonEnum::DISABLE])
            || !isset($params['pay_scene_data']) || empty($params['pay_scene_data'])
            || !isset($params['api_params']) || empty($params['api_params'])
        ) {
            error_exit(SysCode::MISS_REQUIRED_PARAMETER);
        }
        if(PayChannelService::savePayChannelInfo($params)){
            return 'true';
        }else{
            return 'false';
        }
    }

    // 开启和关闭支付渠道, 接入和取消接入支付通道
    public function updatePayChannel(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'status' => 'required',
        ]);
        $params = $request->input();
        if (!isset($params['id']) || empty($params['id'])
            || (isset($params['status']) && !in_array($params['status'], [CommonEnum::ENABLED, CommonEnum::DISABLE]))
            || (isset($params['pay_scene_data']) && empty($params['pay_scene_data']))
        ) {
            error_exit(SysCode::MISS_REQUIRED_PARAMETER);
        }
        if( PayChannelService::updatePayChannel($params)){
            return 'true';
        }else{
            return 'false';
        }
    }
}
